//
//  GFMagazineViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFMagazineViewController.h"

@interface GFMagazineViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *magazineTableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger cityId;
@property (nonatomic, strong) NSMutableArray *arrayForMagazine;
@end

@implementation GFMagazineViewController
#pragma mark - 视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.cityId = [self.city_id integerValue];
    [self viewForNavi];
    [self creatTableView];
    [self.view bringSubviewToFront:self.waitView];
    [self handleData];
}
#pragma mark - 导航
- (void)viewForNavi {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, ScreenHeight / 3, ScreenWidth / 3, ScreenHeight / 4)];
    label.text = @"美食杂志";
    label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    self.view.backgroundColor = [UIColor lightGrayColor];
}
#pragma mark - 解析数据
- (void)handleData {
    if (1 == self.page) {
        self.arrayForMagazine = [NSMutableArray array];
    }
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/foodzine/weekly/list/status=online&page=%ld&city_id=%ld", (long)self.page, (long)self.cityId];
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:result];
        NSArray *array = [dictionary objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            GFMagazineModel *model = [GFMagazineModel modWithDic:dic];
            [self.arrayForMagazine addObject:model];
            if (self.arrayForMagazine.count != 0) {
                [self.magazineTableView reloadData];
            }
        }
        if (array.count != 0) {
            if (self.arrayForMagazine.count < 10) {
                self.magazineTableView.footer.hidden = YES;
            } else {
                self.magazineTableView.footer.hidden = NO;
            }
        } else {
            self.magazineTableView.footer.hidden = YES;
        }
        [self.waitView removeFromSuperview];
        [self.overTimeView removeFromSuperview];
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.view.frame withButtonBlock:^(UIButton *button) {
            [self.view addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.view addSubview:self.overTimeView];
    }];
}
#pragma mark - 创建tableView
- (void)creatTableView {
    self.magazineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.magazineTableView.delegate = self;
    self.magazineTableView.dataSource = self;
    self.magazineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.magazineTableView registerClass:[GFMagazineTableViewCell class] forCellReuseIdentifier:@"magazine"];
    self.magazineTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refresh:)];
    self.magazineTableView.footer.hidden = YES;
    self.magazineTableView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.magazineTableView];
}
#pragma mark - 上拉加载
- (void)refresh:(MJRefreshFooter *)footer {
    [self.view addSubview:self.waitView];
    self.page ++;
    [self.magazineTableView.footer beginRefreshing];
    [self.magazineTableView.footer endRefreshing];
    [self handleData];
}
#pragma mark - row number
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayForMagazine.count;
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GFMagazineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"magazine"];
    if (self.arrayForMagazine.count != 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = [self.arrayForMagazine objectAtIndex:indexPath.row];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GFNearHomeViewController *nearHomeVC = [[GFNearHomeViewController alloc] init];
    nearHomeVC.zineID = 1;
    nearHomeVC.foodzine_id = [self.arrayForMagazine[indexPath.row] foodzine_id];
    [self.navigationController pushViewController:nearHomeVC animated:YES];
}

#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ScreenHeight / 3;
}
@end
